<%@ page language="java" contentType="text/html; charset=utf-8"
		 pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title></title>
	<link href="../css/eys_stly.css" rel="stylesheet" type="text/css" />
	<link href="../css/bootstrap.css" rel="stylesheet" type="text/css" />
	<link href="../ss/css/bootstrap-table.css" rel="stylesheet" type="text/css" />

</head>
<body>
<script src="../js/jquery.js"></script>
<script src="../js/mustache.js"></script>
<script src="../js/bootstrap.min.js"></script>
<div class=" index_buy">
	<h5 class=" h5_all_s">所在位置：<a>添加销售单</a></h5>

	<title>销售单管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">

        function addRow(list, idx, tpl, row){
            $(list).append(Mustache.render(tpl, {
                idx: idx, delBtn: true, row: row
            }));
            $(list+idx).find("select").each(function(){
                $(this).val($(this).attr("data-value"));
            });

        }
        function delRow(obj, prefix){
            var id = $(prefix+"_id");
            var delFlag = $(prefix+"_delFlag");
            if (id.val() == ""){
                $(obj).parent().parent().remove();
            }else if(delFlag.val() == "0"){
                delFlag.val("1");
                $(obj).html("&divide;").attr("title", "撤销删除");
                $(obj).parent().parent().addClass("error");
            }else if(delFlag.val() == "1"){
                delFlag.val("0");
                $(obj).html("&times;").attr("title", "删除");
                $(obj).parent().parent().removeClass("error");
            }
        }

        function changetprica(i) {
            var price=$("#xsglSaleTDetailList"+i+"_price").val();
            if(price.length==0||price==0){
                alert("单价必填");
                return false;
            }
            var amount=$("#xsglSaleTDetailList"+i+"_amount").val();
            if(amount.length==0||amount==0){
                alert("数量必填");
                return false;
            }
            tzk();

        }
        function tzk(){
            for (var i=0;i<xsglSaleTDetailRowIdx;i++){
                var _marketPrice=$("#xsglSaleTDetailList"+i+"_amount").val();
                var _price=$("#xsglSaleTDetailList"+i+"_price").val();
                $("#xsglSaleTDetailList"+i+"_totalprice").val(_price*_marketPrice);

            }
        }
        function changeSettlementPrice(i) {
            var price=$("#xsglSaleHDetailList"+i+"_price").val();
            if(price.length==0||price==0){
                alert("单价必填");
                return false;
            }
            var amount=$("#xsglSaleHDetailList"+i+"_amount").val();
            if(amount.length==0||amount==0){
                alert("数量必填");
                return false;
            }
            zk();

        }

        function changecost(i) {
            var price=$("#xsglSaleHDetailList"+i+"_cost").val();
            if(price.length==0||price==0){
                alert("成本必填");
                return false;
            }
            var amount=$("#xsglSaleHDetailList"+i+"_totalprice").val();
            if(amount.length==0||amount==0){
                alert("总价格必填");
                return false;
            }
            profit();

        }
        function profit(){
            for (var i=0;i<xsglSaleDetailRowIdx;i++){
                var _marketPrice=$("#xsglSaleHDetailList"+i+"_cost").val();
                var _price=$("#xsglSaleHDetailList"+i+"_totalprice").val();
                $("#xsglSaleHDetailList"+i+"_profit").val(_price-_marketPrice);

            }
        }
        function zk(){
            for (var i=0;i<xsglSaleDetailRowIdx;i++){
                var _marketPrice=$("#xsglSaleHDetailList"+i+"_amount").val();
                var _price=$("#xsglSaleHDetailList"+i+"_price").val();
                $("#xsglSaleHDetailList"+i+"_totalprice").val(_price*_marketPrice);

            }
        }
	</script>

	<body>

	<form id="inputForm"  action="${ctx}/xsd_xsglSaleChange/save" method="post" class="form-horizontal">

		<div class="control-group">
			<label class="control-label">编码：</label>
			<div class="controls">
				<input name="code" htmlEscape="false"   class="input-xlarge "/>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label">往来单位</label>
			<div class="controls">
				<select name="company" class="company">
					<c:forEach items="${companys}" var="mc" >
						<option value="${mc.id}"> ${mc.name}</option>
					</c:forEach>
				</select>

			</div>
		</div>
		<div class="control-group">
			<label class="control-label">销售类型</label>
			<div class="controls">
				<select name="kind" class="kind">
					<c:forEach items="${kinds}" var="mc" >
						<option value="${mc.id}"> ${mc.name}</option>
					</c:forEach>
				</select>

			</div>
		</div>
		<div class="control-group">
			<label class="control-label">单据日期：</label>
			<div class="controls">
				<input name="cdate" type="date"  />
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">客户名称：</label>
			<div class="controls">
				<input name="customerName" htmlEscape="false"  class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">销售部门：</label>
			<div class="controls">
				<input name="dept" htmlEscape="false"   class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">销售员：</label>
			<div class="controls">
				<input name="user" htmlEscape="false"    class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">收件人：</label>
			<div class="controls">
				<input name="lxr" htmlEscape="false"   class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">收件电话：</label>
			<div class="controls">
				<input name="tel" htmlEscape="false"   class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">收件地址：</label>
			<div class="controls">
				<input name="address" htmlEscape="false" class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">快递公司：</label>
			<div class="controls">

				<select name="kd" class="kd">
					<c:forEach items="${logistics}" var="mc" >
						<option value="${mc.name}"> ${mc.name}</option>
					</c:forEach>
				</select>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">快递单号：</label>
			<div class="controls">
				<input name="kddh" htmlEscape="false" class="input-xlarge "/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">退货明细：</label>
			<div class="controls">
				<table id="contentTable" class="table table-striped table-bordered table-condensed">
					<thead>
					<tr>
						<th class="hide"></th>
						<th>商品编码</th>
						<th>商品名称</th>
						<th>数量</th>
						<th>单价</th>
						<th>总价格</th>

						<th width="10">&nbsp;</th>
					</tr>
					</thead>
					<tbody id="xsglSaleTDetailList">
					</tbody>
					<tfoot>
					<tr><td colspan="10"><a href="javascript:" onclick="addRow('#xsglSaleTDetailList', xsglSaleTDetailRowIdx, xsglSaleTDetailTpl);xsglSaleTDetailRowIdx = xsglSaleTDetailRowIdx + 1;" class="btn">新增</a></td></tr>
					</tfoot>
				</table>
				<script type="text/template" id="xsglSaleTDetailTpl">//<!--
						<tr id="xsglSaleTDetailList{{idx}}">
							<td class="hide">
								<input id="xsglSaleTDetailList{{idx}}_id" name="xsglSaleTDetailList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
								<input id="xsglSaleTDetailList{{idx}}_delFlag" name="xsglSaleTDetailList[{{idx}}].delFlag" type="hidden" value="0"/>
							</td>
							<td>
								<input id="xsglSaleTDetailList{{idx}}_code" name="xsglSaleTDetailList[{{idx}}].code" type="text" value="{{row.code}}" maxlength="20" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleTDetailList{{idx}}_name" name="xsglSaleTDetailList[{{idx}}].name" type="text" value="{{row.name}}" maxlength="11" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleTDetailList{{idx}}_amount" name="xsglSaleTDetailList[{{idx}}].amount" type="text" value="{{row.amount}}" maxlength="11" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleTDetailList{{idx}}_price" onchange="changetprica({{idx}})" name="xsglSaleTDetailList[{{idx}}].price" type="text" value="{{row.price}}" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleTDetailList{{idx}}_totalprice" readonly name="xsglSaleTDetailList[{{idx}}].totalprice" type="text" value="{{row.totalprice}}" class="input-small "/>
							</td>

							 <td class="text-center" width="10">
								{{#delBtn}}<span class="close" onclick="delRow(this, '#xsglSaleTDetailList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
							</td>
						</tr>//-->
				</script>
				<script type="text/javascript">
                    var xsglSaleTDetailRowIdx = 0, xsglSaleTDetailTpl = $("#xsglSaleTDetailTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
                    $(document).ready(function() {
                        var data = ${fns:toJson(xsglSaleChange.xsglSaleTDetailList)};
                        for (var i=0; i<data.length; i++){
                            addRow('#xsglSaleTDetailList', xsglSaleTDetailRowIdx, xsglSaleTDetailTpl, data[i]);
                            xsglSaleTDetailRowIdx = xsglSaleTDetailRowIdx + 1;
                        }
                    });
				</script>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">换货明细：</label>
			<div class="controls">
				<table id="contentTable" class="table table-striped table-bordered table-condensed">
					<thead>
					<tr>
						<th class="hide"></th>
						<th>商品编码</th>
						<th>商品名称</th>
						<th>数量</th>
						<th>单价</th>
						<th>总价格</th>
						<th>成本</th>
						<th>毛利</th>
						<th width="10">&nbsp;</th>
					</tr>
					</thead>
					<tbody id="xsglSaleHDetailList">
					</tbody>
					<tfoot>
					<tr><td colspan="7"><a href="javascript:" onclick="addRow('#xsglSaleHDetailList', xsglSaleDetailRowIdx, xsglSaleDetailTpl);xsglSaleDetailRowIdx = xsglSaleDetailRowIdx + 1;" class="btn">新增</a></td></tr>
					</tfoot>
				</table>
				<script type="text/template" id="xsglSaleDetailTpl">//<!--
						<tr id="xsglSaleHDetailList{{idx}}">
							<td class="hide">
								<input id="xsglSaleHDetailList{{idx}}_id" name="xsglSaleHDetailList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
								<input id="xsglSaleHDetailList{{idx}}_delFlag" name="xsglSaleHDetailList[{{idx}}].delFlag" type="hidden" value="0"/>
							</td>
							<td>
								<input id="xsglSaleHDetailList{{idx}}_code" name="xsglSaleHDetailList[{{idx}}].code" type="text" value="{{row.code}}" maxlength="20" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleHDetailList{{idx}}_name" name="xsglSaleHDetailList[{{idx}}].name" type="text" value="{{row.name}}" maxlength="11" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleHDetailList{{idx}}_amount" name="xsglSaleHDetailList[{{idx}}].amount" type="text" value="{{row.amount}}" maxlength="11" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleHDetailList{{idx}}_price" name="xsglSaleHDetailList[{{idx}}].price" onchange="changeSettlementPrice({{idx}})" type="text" value="{{row.price}}" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleHDetailList{{idx}}_totalprice" name="xsglSaleHDetailList[{{idx}}].totalprice" readonly type="text" value="{{row.totalprice}}" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleHDetailList{{idx}}_cost" name="xsglSaleHDetailList[{{idx}}].cost" onchange="changecost({{idx}})" type="text" value="{{row.cost}}" class="input-small "/>
							</td>
							<td>
								<input id="xsglSaleHDetailList{{idx}}_profit" name="xsglSaleHDetailList[{{idx}}].profit" readonly type="text" value="{{row.profit}}" class="input-small "/>
							</td>
						 <td class="text-center" width="10">
								{{#delBtn}}<span class="close" onclick="delRow(this, '#xsglSaleHDetailList{{idx}}')" title="删除">&times;</span>{{/delBtn}}
							</td>
						</tr>//-->
				</script>
				<script type="text/javascript">
                    var xsglSaleDetailRowIdx = 0, xsglSaleDetailTpl = $("#xsglSaleDetailTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
                    $(document).ready(function() {
                        var data = ${fns:toJson(xsglSale.xsglSaleHDetailList)};
                        for (var i=0; i<data.length; i++){
                            addRow('#xsglSaleHDetailList', xsglSaleDetailRowIdx, xsglSaleDetailTpl, data[i]);
                            xsglSaleDetailRowIdx = xsglSaleDetailRowIdx + 1;
                        }
                    });
				</script>
			</div>
		</div>


		<div class="form-actions">
			<input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form>

	</body>
</html>